home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / smaltalk.lha / smalltalk-1.1.1 / Collection.st < prev    next >
Text File  |  1991-09-12  |  7KB  |  267 lines

  1. "======================================================================
  2. |
  3. |   Collection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         12 Sep 91      Changed printOn: to recursively call printOn:
  34. |
  35. | sbb         12 Sep 91      Fix detect: method to return the value
  36. |
  37. | sbyrne     25 Apr 89      created.
  38. |
  39. "
  40.  
  41. Object subclass: #Collection
  42.        instanceVariableNames: ''
  43.        classVariableNames: ''
  44.        poolDictionaries: ''
  45.        category: nil.
  46.  
  47. Collection comment: 
  48. 'I am an abstract class.  My instances are collections of objects.  My
  49. subclasses may place some restrictions or add some definitions to how
  50. the objects are stored or organized; I say nothing about this.  I merely
  51. provide some object creation and access routines for general collections
  52. of objects.' !
  53.  
  54.  
  55. !Collection class methodsFor: 'instance creation'!
  56.  
  57. with: anObject
  58.     ^self new add: anObject; yourself
  59. !
  60.  
  61. with: firstObject with: secondObject
  62.     ^self new add: firstObject; add: secondObject; yourself
  63. !
  64.  
  65. with: firstObject with: secondObject with: thirdObject
  66.     ^self new add: firstObject; add: secondObject; add: thirdObject; yourself
  67. !
  68.  
  69. with: firstObject with: secondObject with: thirdObject with: fourthObject
  70.     ^self new add: firstObject; add: secondObject; add: thirdObject;
  71.         add: fourthObject; yourself
  72. !!
  73.  
  74.  
  75.  
  76. !Collection methodsFor: 'Adding to a collection'!
  77.  
  78. add: newObject
  79.     self subclassResponsibility
  80. !
  81.  
  82. addAll: aCollection
  83.     aCollection do: [ :element | self add: element ].
  84.     ^aCollection
  85. !!
  86.  
  87.  
  88.  
  89. !Collection methodsFor: 'Removing from a collection'!
  90.  
  91. remove: oldObject ifAbsent: anExceptionBlock
  92.     self subclassResponsibility
  93. !
  94.  
  95. remove: oldObject
  96.     self remove: oldObject
  97.          ifAbsent: [ ^self error: 'Failed to remove object' ].
  98.     ^oldObject
  99. !
  100.  
  101. removeAll: aCollection
  102.     " ??? we're supposed to report an error if an object can't be removed
  103.       properly.  I've elected to let remove: report the error.  Also, it's
  104.       not clear whether we're supposed to remove all occurrences of the
  105.       members of aCollection from the receiver, or only the first."
  106.     aCollection do: [ :element | self remove: element ].
  107.     ^aCollection
  108. !!
  109.  
  110.  
  111.  
  112. !Collection methodsFor: 'testing collections'!
  113.  
  114. includes: anObject
  115.     self do: [ :element | anObject = element ifTrue: [ ^true ] ].
  116.     ^false
  117. !
  118.  
  119. isEmpty
  120.     ^self size == 0
  121. !
  122.  
  123. occurrencesOf: anObject
  124.     | count |
  125.     count _ 0.
  126.     self do: [ :element | anObject == element ifTrue: [ count _ count + 1 ] ].
  127.     ^count
  128. !
  129.  
  130. size
  131.     | count |
  132.     count _ 0.
  133.     self do: [ :element | count _ count + 1].
  134.     ^count
  135. !!
  136.  
  137.  
  138.  
  139. !Collection methodsFor: 'enumerating the elements of a collection'!
  140.  
  141. do: aBlock
  142.     self subclassResponsibility
  143. !
  144.  
  145. select: aBlock
  146.     | newCollection |
  147.     " ### I don't know if this is the right way to create a new collection
  148.       in all cases...I suspect that it is not..."
  149.     newCollection _ self species new.
  150.     self do: [ :element | (aBlock value: element)
  151.                             ifTrue: [ newCollection add: element ]
  152.          ].
  153.     ^newCollection
  154. !
  155.  
  156. reject: aBlock
  157.     | newCollection |
  158.     " ### I don't know if this is the right way to create a new collection
  159.       in all cases...I suspect that it is not..."
  160.     newCollection _ self species new.
  161.     self do: [ :element | (aBlock value: element)
  162.                             ifFalse: [ newCollection add: element ]
  163.          ].
  164.     ^newCollection
  165. !
  166.  
  167. collect: aBlock
  168.     | newCollection |
  169.     " ### I don't know if this is the right way to create a new collection
  170.       in all cases...I suspect that it is not..."
  171.     newCollection _ self species new.
  172.     self do: [ :element | newCollection add: (aBlock value: element) ].
  173.     ^newCollection
  174. !
  175.  
  176. detect: aBlock ifNone: exceptionBlock
  177.     self do: [ :element | (aBlock value: element) ifTrue: [ ^element ] ].
  178.     exceptionBlock value    
  179. !
  180.     
  181. detect: aBlock
  182.     ^self detect: aBlock ifNone: [ ^self error: 'Collection detect: failed']
  183. !
  184.  
  185. inject: thisValue into: binaryBlock
  186.     self do: [ :element | thisValue _ binaryBlock value: thisValue
  187.                                                   value: element ].
  188.     ^thisValue
  189. !!
  190.  
  191.  
  192.  
  193. !Collection methodsFor: 'converting'!
  194.  
  195. asBag
  196.     | aBag |
  197.     aBag _ Bag new.
  198.     self do: [ :element | aBag add: element ].
  199.     ^aBag
  200. !
  201.  
  202. asSet
  203.     | aSet |
  204.     aSet _ Set new: self size.
  205.     self do: [ :element | aSet add: element ].
  206.     ^aSet
  207. !
  208.  
  209. asOrderedCollection
  210.     | anOrderedCollection |
  211.     anOrderedCollection _ OrderedCollection new: self size.
  212.     self do: [ :element | anOrderedCollection add: element ].
  213.     ^anOrderedCollection
  214. !
  215.  
  216. asSortedCollection
  217.     | aSortedCollection |
  218.     aSortedCollection _ SortedCollection new.
  219.     self do: [ :element | aSortedCollection add: element ].
  220.     ^aSortedCollection
  221. !
  222.  
  223. asSortedCollection: aBlock
  224.     | aSortedCollection |
  225.     aSortedCollection _ SortedCollection sortBlock: aBlock.
  226.     self do: [ :element | aSortedCollection add: element ].
  227.     ^aSortedCollection
  228. !!
  229.  
  230.  
  231.  
  232. !Collection methodsFor: 'printing'!
  233.  
  234. printOn: aStream
  235.     | firstTime |
  236.     firstTime _ true.
  237.     aStream nextPutAll: self classNameString.
  238.     aStream nextPutAll: ' ('.
  239.     self do:
  240.         [ :element | firstTime ifTrue: [ firstTime _ false ]
  241.                            ifFalse: [ aStream nextPut: Character space ].
  242.               element printOn: aStream ].
  243.     aStream nextPut: $)
  244. !!
  245.  
  246.  
  247.  
  248. !Collection methodsFor: 'storing'!
  249.  
  250. store: aStream
  251.     | noElements |
  252.     aStream nextPut: $(.
  253.     aStream nextPutAll: self classNameString.
  254.     aStream nextPutAll: ' new'.
  255.     noElements _ true.
  256.     self do:
  257.         [ :element | aStream nextPutAll: ' add: '.
  258.                      element storeOn: aStream.
  259.              aStream nextPut: $;.
  260.              noElements _ false ].
  261.     noElements ifFalse: [ aStream nextPutAll: ' yourself' ].
  262.     aStream nextPut: $)
  263. !!
  264.  
  265.